﻿<h1>Databound Custom Control Example</h1>
This example explains how to create a custom control (customdatapicker). This custom control is based on the jQuery Datapicker control.<br>
<button id="CustomControlExample" class="try-example-button">Try Example</button>
<h2>Creation of a custom control requires the following steps:</h2>
1. Create a template for the custom control and save it in the template folder under the name of custom control type + extension ".htm". In this example the template file is named "customdatapicker.htm", because custom control type is "customdatapicker".
<ul><li style="margin-left: 32px">Use the <a href="https://github.com/janl/mustache.js">moustache</a> syntax to refer to the model properties.</li>
<li style="margin-left: 32px">Apply <i>redui-onchangeupdatesdata</i> class to indicate that the element updates the binding context.</li></ul>

<h2>Template</h2>
<div style="color:Black;background-color:White;"><pre><span style="color:Blue;">&lt;</span><span style="color:#A31515;">div</span> <span style="color:Red;">id</span><span style="color:Blue;">=</span><span style="color:Blue;">"{{id}}_template"</span> <span style="color:Red;">class</span><span style="color:Blue;">=</span><span style="color:Blue;">"redui-customdatapicker-outer redui-valid{{#model.cssClass}} {{model.cssClass}}{{/model.cssClass}}{{#model.isHidden}} redui-hidden{{/model.isHidden}}"</span><span style="color:Blue;">&gt;</span>
  {{#model.label}}<span style="color:Blue;">&lt;</span><span style="color:#A31515;">label</span> <span style="color:Red;">for</span><span style="color:Blue;">=</span><span style="color:Blue;">"{{id}}"</span> <span style="color:Red;">class</span><span style="color:Blue;">=</span><span style="color:Blue;">"redui-customdatapicker-label"</span><span style="color:Blue;">&gt;</span>
    <span style="color:Blue;">&lt;</span><span style="color:#A31515;">span</span><span style="color:Blue;">&gt;</span>{{#_localize}}{{model.label}}{{/_localize}}: {{#model.isRequired}}*{{/model.isRequired}}<span style="color:Blue;">&lt;/</span><span style="color:#A31515;">span</span><span style="color:Blue;">&gt;</span>
  <span style="color:Blue;">&lt;/</span><span style="color:#A31515;">label</span><span style="color:Blue;">&gt;</span>{{/model.label}}
  <span style="color:Blue;">&lt;</span><span style="color:#A31515;">input</span> <span style="color:Red;">id</span><span style="color:Blue;">=</span><span style="color:Blue;">"{{id}}"</span> <span style="color:Red;">name</span><span style="color:Blue;">=</span><span style="color:Blue;">"{{name}}"</span> <span style="color:Red;">data-name</span><span style="color:Blue;">=</span><span style="color:Blue;">"{{name}}"</span> <span style="color:Red;">class</span><span style="color:Blue;">=</span><span style="color:Blue;">"redui-customdatapicker redui-focusable"</span> <span style="color:Red;">type</span><span style="color:Blue;">=</span><span style="color:Blue;">"text"</span> <span style="color:Blue;">/&gt;</span>
  <span style="color:Blue;">&lt;</span><span style="color:#A31515;">div</span> <span style="color:Red;">id</span><span style="color:Blue;">=</span><span style="color:Blue;">"{{id}}_validationerrorbox"</span> <span style="color:Red;">class</span><span style="color:Blue;">=</span><span style="color:Blue;">"redui-validation-errorbox"</span><span style="color:Blue;">&gt;</span><span style="color:Blue;">&lt;/</span><span style="color:#A31515;">div</span><span style="color:Blue;">&gt;</span>
<span style="color:Blue;">&lt;/</span><span style="color:#A31515;">div</span><span style="color:Blue;">&gt;</span>
</pre></div><br><br><br>2. Declare the custom control in the model. Use "custom" as a "type" property value, and specify the custom control type as a "customType" property value. In this example custom type is "customdatapicker" (As mentioned before, should match the template).<br>
<h2>UI Model</h2>
<div style="color:Black;background-color:White;"><pre>{
    <span style="color:#A31515;">"windows"</span>: [
        {
            <span style="color:#A31515;">"name"</span>: <span style="color:#A31515;">"CustomControlExample"</span>,
            <span style="color:#A31515;">"title"</span>: <span style="color:#A31515;">"Custom Control Example"</span>,
            <span style="color:#A31515;">"left"</span>: 500,
            <span style="color:#A31515;">"top"</span>: 100,
            <span style="color:#A31515;">"width"</span>: 400,
            <span style="color:#A31515;">"height"</span>: 300,
            <span style="color:#A31515;">"controls"</span>: [
                <b>{
                    <span style="color:#A31515;">"name"</span>: <span style="color:#A31515;">"customControl"</span>,
                    <span style="color:#A31515;">"type"</span>: <span style="color:#A31515;">"custom"</span>,
                    <span style="color:#A31515;">"customType"</span>: <span style="color:#A31515;">"customdatapicker"</span>,
                    <span style="color:#A31515;">"label"</span>: <span style="color:#A31515;">"Date"</span>,
                    <span style="color:#A31515;">"bindsTo"</span>: <span style="color:#A31515;">"selectedDate"</span>,
                    <span style="color:#A31515;">"isRequired"</span>: <span style="color:Blue;">true</span>
                }</b>,
                {
                    <span style="color:#A31515;">"name"</span>: <span style="color:#A31515;">"OKButton"</span>,
                    <span style="color:#A31515;">"type"</span>: <span style="color:#A31515;">"button"</span>,
                    <span style="color:#A31515;">"text"</span>: <span style="color:#A31515;">"OK"</span>,
                    <span style="color:#A31515;">"isDialogButton"</span>: <span style="color:Blue;">true</span>,
                    <span style="color:#A31515;">"dialogResult"</span>: <span style="color:#A31515;">"OK"</span>
                },
                {
                    <span style="color:#A31515;">"name"</span>: <span style="color:#A31515;">"CancelButton"</span>,
                    <span style="color:#A31515;">"type"</span>: <span style="color:#A31515;">"button"</span>,
                    <span style="color:#A31515;">"text"</span>: <span style="color:#A31515;">"Cancel"</span>,
                    <span style="color:#A31515;">"isDialogButton"</span>: <span style="color:Blue;">true</span>,
                    <span style="color:#A31515;">"dialogResult"</span>: <span style="color:#A31515;">"Cancel"</span>
                }
            ]
        }
    ]
}
</pre></div><br><br><br>3. Define a handler for the data binding as a property of a <i>redui.customControlHandlers</i> object. The handler name should match the custom type. In this example handler name is "customdatapicker". Handler should be an object with 2 methods: "bind" and "getValue".
<ul><li style="margin-left: 32px">Implement "getValue(control: ICustomControl): any" method on the handler.</li>
<li style="margin-left: 32px">Implement "setValue(control: ICustomControl, value: any): void" method on the handler.</li>
<li style="margin-left: 32px">Implement "activate(control: ICustomControl): void" method on the handler.</li></ul>

<h2>Data Model</h2>
<div style="color:Black;background-color:White;"><pre><span style="color:Blue;">var</span> data = {
    selectedDate: <span style="color:#A31515;">"01/15/2014"</span>
};
</pre></div>
<h2>Code</h2>
<div style="color:Black;background-color:White;"><pre>redui.customControlHandlers.customdatapicker = {
	getValue: <span style="color:Blue;">function</span> (control) {
		<span style="color:Blue;">return</span> control.getElement().val();
	},
	setValue: <span style="color:Blue;">function</span> (control, value) {
		control.getElement().val(value);
	},
	activate: <span style="color:Blue;">function</span> (control) {
		control.getElement().datepicker();
	}
};

<span style="color:Blue;">var</span> exampleWindow = redui.createNewWindow(<span style="color:#A31515;">"CustomControlExample"</span>);
exampleWindow.bind(data);
exampleWindow.show();

exampleWindow.closed(<span style="color:Blue;">function</span> (target, dialogResult) {
	<span style="color:Blue;">if</span> (dialogResult === <span style="color:#A31515;">"OK"</span>) {
		alert(<span style="color:#A31515;">"Model data: "</span> + JSON.stringify(data));
	}
});
</pre></div>